package com.yj.web.util;

import com.thoughtworks.xstream.XStream;
import com.yj.web.WeChat.Message.WxXmlMessage;
import com.yj.web.WeChat.Message.WxXmlOutMessage;
import com.yj.web.WeChat.Message.WxXmlOutNewsMessage;
import com.yj.web.WeChat.Message.WxXmlOutTextMessage;

import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by yanlinyang on 2017/9/10.
 */
public class XStreamTransformer {
    protected static final Map<Class, XStream> CLASS_2_XSTREAM_INSTANCE = configXStreamInstance();

    @SuppressWarnings("unchecked")
    public static <T> T fromXml(Class<T> clazz, String xml) {
        T object = (T) CLASS_2_XSTREAM_INSTANCE.get(clazz).fromXML(xml);
        return object;
    }

    @SuppressWarnings("unchecked")
    public static <T> T fromXml(Class<T> clazz, InputStream is) {
        T object = (T) CLASS_2_XSTREAM_INSTANCE.get(clazz).fromXML(is);
        return object;
    }

    public static <T> String toXml(Class<T> clazz, T object) {
        return CLASS_2_XSTREAM_INSTANCE.get(clazz).toXML(object);
    }

    private static Map<Class, XStream> configXStreamInstance() {
        Map<Class, XStream> map = new HashMap<Class, XStream>();
        map.put(WxXmlMessage.class, config_WxXmlMessage());
//        map.put(WxMpXmlOutMusicMessage.class, config_WxMpXmlOutMusicMessage());
        map.put(WxXmlOutNewsMessage.class, config_WxXmlOutNewsMessage());
        map.put(WxXmlOutTextMessage.class, config_WxMpXmlOutTextMessage());
//        map.put(WxMpXmlOutImageMessage.class, config_WxMpXmlOutImageMessage());
//        map.put(WxMpXmlOutVideoMessage.class, config_WxMpXmlOutVideoMessage());
//        map.put(WxMpXmlOutVoiceMessage.class, config_WxMpXmlOutVoiceMessage());
//        map.put(WxMpXmlOutTransferCustomerServiceMessage.class, config_WxMpXmlOutTransferCustomerServiceMessage());

        return map;
    }

    private static XStream config_WxXmlMessage() {
        XStream xstream = XStreamInitializer.getInstance();
        xstream.processAnnotations(WxXmlMessage.class);
//        xstream.processAnnotations(WxXmlMessage.ScanCodeInfo.class);
//        xstream.processAnnotations(WxXmlMessage.SendPicsInfo.class);
//        xstream.processAnnotations(WxXmlMessage.SendPicsInfo.Item.class);
//        xstream.processAnnotations(WxXmlMessage.SendLocationInfo.class);

        xstream.aliasField("MsgID", WxXmlMessage.class, "msgId");
        return xstream;
    }

    private static XStream config_WxXmlOutNewsMessage() {
        XStream xstream = XStreamInitializer.getInstance();
        xstream.processAnnotations(WxXmlOutMessage.class);
        xstream.processAnnotations(WxXmlOutNewsMessage.class);
        xstream.processAnnotations(WxXmlOutNewsMessage.Item.class);
        return xstream;
    }

    private static XStream config_WxMpXmlOutTextMessage() {
        XStream xstream = XStreamInitializer.getInstance();
        xstream.processAnnotations(WxXmlOutMessage.class);
        xstream.processAnnotations(WxXmlOutTextMessage.class);
        return xstream;
    }

}
